﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Text;

namespace BlockingIO
{
    public class BIOSerial : BIO
    {
        public SerialPort SerialPort;

        public BIOSerial(SerialPort serialPort)
        {
            this.SerialPort = serialPort;
            this.mStream = serialPort.BaseStream;
            this.FunctionTryConnect = this.SerialTryConnect;
        }

        public BIOSerial(SerialPort serialPort, Encoding encoding)
        {
            SerialPort = serialPort;
            this.FunctionTryConnect = this.SerialTryConnect;
        }

        private bool SerialTryConnect()
        {
            if (this.SerialPort != null && this.SerialPort.IsOpen)
                return true;

            try
            {
                this.SerialPort.Open();
                return true;
            }
            catch (Exception err)
            {
                return false;
            }
        }
    }
}
